package com.jtlrm.ckd.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.ValidateUtils;
import com.base.sdk.widget.TitleBar;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.BaseActivity;
import com.jtlrm.ckd.entity.UserEntity;
import com.jtlrm.ckd.entity.YanZhengMaEntity;
import com.jtlrm.ckd.entity.event.MainAcitivityFinishMessage;
import com.jtlrm.ckd.entity.event.UserInfoMessage;
import com.jtlrm.ckd.mvp.model.UserModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.util.EventBusUtil;

import butterknife.BindView;

/**
 * 修改电话号码
 */
public class ChangePhoneActivity extends BaseActivity {
    @BindView(R.id.change_phone_title)
    TitleBar titleBar;
    @BindView(R.id.phone)
    EditText phoneE;
    @BindView(R.id.yanzhengma)
    EditText yanZhengMaE;
    @BindView(R.id.get_yanzhengma)
    TextView getYanZhengMaE;
    boolean canSend = true;
    String phone;
    UserModel userModel;
    String yanZhengMa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_change_phone);
    }

    @Override
    protected void initView() {
        titleBar.tvMiddle.setText("修改手机号");
        titleBar.imgLeft.setVisibility(View.VISIBLE);
        titleBar.imgLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        userModel = new UserModel();
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        getYanZhengMaE.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!canSend) {
                    return;
                }
                phone = phoneE.getText() + "";
                if (ValidateUtils.Mobile(phone)) {
                    sendMessage();
                } else {
                    showToast("请输入正确的手机号");
                }
            }
        });
    }

    private void sendMessage() {
        userModel.sendMessage(phone, new CommonObserver<YanZhengMaEntity>() {
            @Override
            public void onError(int errType, String errMessage) {
                dismissLoadingDialog();
                showToast(errMessage);
                setYanzhenMaStatus(true);
            }

            @Override
            public void onResult(YanZhengMaEntity data) {
                dismissLoadingDialog();
                showToast("已发送验证，注意查收");
                yanZhengMa = data.getCode();
                setYanzhenMaStatus(false);
            }
        }, lifecycleSubject);
    }

    CountDownTimer countDownTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            getYanZhengMaE.setText((millisUntilFinished / 1000) + "s后重新获取");
        }

        @Override
        public void onFinish() {
            getYanZhengMaE.setText("获取验证码");
            setYanzhenMaStatus(true);
        }
    };

    public void setYanzhenMaStatus(boolean isEdit) {
        if (isEdit) {
            canSend = true;
            getYanZhengMaE.setTextColor(getResourceColor(R.color.text_orange));
        } else {
            countDownTimer.start();
            canSend = false;
            getYanZhengMaE.setTextColor(getResourceColor(R.color.text_grey));
        }
    }

    public void submit(View view) {
        if (!notEmpty(yanZhengMa)) {
            showToast("请获取验证码");
            return;
        }
        if (inputEmpty(yanZhengMaE)) {
            showToast("请输入验证码");
            return;
        }
        if (!yanZhengMa.equals(yanZhengMaE.getText() + "")) {
            showToast("验证码输入有误");
            return;
        }
        if (!phone.equals(phoneE.getText() + "")) {
            showToast("请重新获取验证码");
            return;
        }
        showLoadingDialog("修改中");
        userModel.changePhone(phone, new CommonObserver<Object>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(Object data) {
                showToast("修改成功");
                UserHelper.getInstance(context).clearData();
                startActivity(new Intent(context, LoginActivity.class));
                EventBusUtil.postEvent(new MainAcitivityFinishMessage());
                finish();
             //   updateUserInfo();
            }
        }, lifecycleSubject);
    }

    public void updateUserInfo() {
        userModel.getUserInfo(new CommonObserver<UserEntity>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(UserEntity data) {
                showToast("手机号修改成功");
                dismissLoadingDialog();
                UserHelper.getInstance(context).setUserInfo(data);
                EventBusUtil.postEvent(new UserInfoMessage(true));
            }
        }, lifecycleSubject);
    }
}
